Skip to content

Keep simulated Ctrl+C inside the interrupt tests - #2427

Open
Pierre-Yves Le Borgne (pylaterreur) wants to merge 1 commit into
confluentinc:masterfrom
pylaterreur:fix/wakeable-tests-contain-interrupt
Open

Pierre-Yves Le Borgne (pylaterreur) wants to merge 1 commit into
confluentinc:masterfrom
pylaterreur:fix/wakeable-tests-contain-interrupt

Conversation

@pylaterreur

@pylaterreur Pierre-Yves Le Borgne (pylaterreur) commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What

The interrupt tests in tests/test_Wakeable.py, and two in tests/test_ShareConsumer.py, start a thread that calls _thread.interrupt_main() after a fixed delay, and expect the KeyboardInterrupt inside a try/except around the blocking call. When the call returns first, or the thread wakes up late, the KeyboardInterrupt is raised later: in cleanup, in another test, or in pytest itself, which then aborts the whole session. This happened in the first CI run of #2416 on the macOS x64 agent (job): pytest stopped with KeyboardInterrupt at <frozen posixpath>:457 after 16 tests of test_Wakeable.py, and the rerun passed.

The 17th test, test_can_be_interrupted[producer-poll], never interrupted poll(). Its producer has bootstrap.servers: localhost:9092, and with no broker there, Producer.poll() returns 1 or 2 within a few milliseconds, as soon as it has served the "connection refused" error. The test only passed because the interrupt landed in the 0.2 s sleep of its finally block; an interrupt more than about 0.1 s late escaped.

This adds TestUtils.interrupt_main_after(delay), a context manager for the blocking call. Leaving the block waits for the interrupt, so the KeyboardInterrupt is always raised inside the block; a call that returns before it is interrupted fails with AssertionError: Not interrupted: ... instead. All 23 simulated interrupts use it. The producer in test_can_be_interrupted no longer sets bootstrap.servers, like the producers of the other interrupt tests, so poll() blocks until it is interrupted, and the sleeps that waited for late interrupts are gone. The two 1 s polls interrupted after 0.4 s get the 2 s timeout of the others, so an interrupt can be up to 1.4 s late everywhere (4.6 s in test_ShareConsumer.py).

Checklist

  • Contains customer facing changes? Including API/behavior changes
    • No, tests only.
  • Did you add sufficient unit test and/or integration test coverage for this PR?
    • Test-only change; see below for how it was checked.

References

Test & Review

  • test_can_be_interrupted[producer-poll] on master, traced outside pytest (Python 3.11.16): poll() returned after 1 to 6 ms in all 11 runs, 8 of them under a 10% CPU quota, and the interrupt was always caught by the finally sleep. On this branch, with only the context manager and the old producer config, the test fails every time with AssertionError: Not interrupted: the block ended before the interrupt sent after 0.1s.
  • Late interrupts, injected with a pytest plugin that delays TestUtils.send_sigint_after_delay(), running all 27 interrupt tests (Python 3.11.16, Linux x86_64):
    • master, 0.3 s late: all 4 runs broken. test_can_be_interrupted[producer-poll] failed in 3, and 1 run aborted with KeyboardInterrupt in obj.close() of the next test, after "1 failed, 16 passed", as in CI.
    • this branch, 0.3 s late: 4 runs, all passed. In 4 more runs made while the full unit suite ran in parallel, one failed an elapsed-time bound: poll(timeout=1.0) took 2.51 s in test_calculate_chunk_timeout_utility_function[consumer].
    • this branch, 1.0 s late: 2 runs, all passed.
    • this branch, 2.0 s late, beyond the 1.4 s margin: the 11 tests that interrupt a call with a finite timeout fail with AssertionError: Not interrupted: ..., the other 16 pass, and the session runs to the end.
  • CPU throttling alone did not reproduce the failure on Linux: master passed 30 runs of test_can_be_interrupted under a 5% CPU quota with 500 ms periods. Throttled threads wake together, and the interrupting thread, which wakes first, also runs first.
  • Without injected delays, the 27 tests pass on Python 3.9, 3.11, 3.14 and 3.14t, and test_can_be_interrupted passes 30 runs out of 30 under the 5% CPU quota.
  • tools/style-format.sh on the changed files and flake8 pass.

Open questions / Follow-ups

  • The tests also assert elapsed times, such as poll(timeout=1.0) returning within 2 s. Those bounds can still fail on a loaded machine; this PR does not change them.

🤖 Generated with Claude Code

https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN

@confluent-cla-assistant

Copy link
Copy Markdown

🎉 All Contributor License Agreements have been signed. Ready to merge.
✅ pylaterreur
Please push an empty commit if you would like to re-run the checks to verify CLA status for all contributors.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The interrupt handler can return while a scheduled interrupt is still pending, allowing a second KeyboardInterrupt to escape into cleanup or pytest and abort the session.

1 open issue
What changed in this PR

This test-only PR addresses intermittent CI session aborts caused by simulated Ctrl+C arriving after blocking Kafka calls return.

Changes:

  • Adds a shared interrupt context manager that waits for delayed interrupts and reports premature returns as assertion failures.
  • Migrates 23 interrupt-injection sites across the wakeable and share-consumer tests.
  • Removes cleanup sleeps, increases two poll timeouts, and removes the producer broker configuration that caused polling to return early.
File Description
tests/​test_Wakeable.py Adopts the helper and adjusts poll setup, timeouts, and cleanup.
tests/​test_ShareConsumer.py Uses the helper for finite and infinite poll interrupt tests.
tests/​common/​__init__.py Adds the shared delayed-interrupt context manager.

Review effort: Balanced

Comment thread tests/common/__init__.py Outdated
Comment on lines +157 to +163
if exc_type is KeyboardInterrupt:
return False # Interrupted inside the block, as intended.
# The block ended first. Wait for the interrupt, so that it is raised here, not later.
try:
self._sent.wait()
except KeyboardInterrupt:
pass

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. __exit__ now waits for the scheduled interrupt in every case, including when the block was ended by a different KeyboardInterrupt (a real Ctrl+C); a second KeyboardInterrupt arriving during that wait is swallowed and the original one propagates. The interrupt tests of test_Wakeable.py and test_ShareConsumer.py pass with it (27 passed).

The interrupt tests start a thread that calls _thread.interrupt_main()
after a fixed delay, and expect the KeyboardInterrupt inside a
try/except around the blocking call. When the call returns first, or
the thread wakes up late, the KeyboardInterrupt is raised later: in
cleanup, in another test, or in pytest itself, which then aborts the
whole session. On the macOS x64 CI agent, pytest stopped after 16 tests
of test_Wakeable.py.

The 17th, test_can_be_interrupted[producer-poll], never interrupted
poll(): with bootstrap.servers set and no broker, poll() returns within
a few milliseconds after serving the "connection refused" error. The
test only passed because the interrupt landed in the 0.2 s sleep of its
finally block, and an interrupt more than about 0.1 s late escaped.

Add TestUtils.interrupt_main_after(), a context manager for the
blocking call. Leaving the block waits for the interrupt, so it is
always raised inside the block, and a call that returns before being
interrupted fails with an AssertionError instead. Use it for every
simulated Ctrl+C in test_Wakeable.py and test_ShareConsumer.py, drop
bootstrap.servers from the producer in test_can_be_interrupted so that
poll() blocks until interrupted, and give the two 1 s polls interrupted
after 0.4 s the 2 s timeout of the others.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GwUvhmzgfkiZ6vo4uGyesN
@Ankith-Confluent Ankith L (Ankith-Confluent) added status:pending-close Issue scheduled for closure — comment if still relevant and removed status:pending-close Issue scheduled for closure — comment if still relevant labels Nov 19, 2026
@Ankith-Confluent

Copy link
Copy Markdown
Member

Thanks for opening this PR. This is a test-only change. If it fixes a failing or flaky CI run, please link an example.

@pylaterreur

Pierre-Yves Le Borgne (pylaterreur) commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor Author

Ankith L (@Ankith-Confluent) here are the CI runs this fixes, both on commits that don't contain this change. In each the macOS x64 job has pytest stopped by KeyboardInterrupt (<frozen posixpath>:457) right after the first 16 tests of tests/test_Wakeable.py, and the rerun passed:

Both are listed under "References" in the description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:pending-close Issue scheduled for closure — comment if still relevant

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants